/*
 * Copyright 2008 Johnny Jian
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.getris.game.color

import com.googlecode.getris.game.Container
import java.awt.Color

class ColorfulQuadrelUtils {

	public static final COLORS = [
		[0, 200, 255],
		[255, 0, 0],
		[255, 150, 0],
		[0, 200, 0],
		[40, 0, 240],
		[210, 0, 240],
		[255, 255, 0]
	]

    static {
        COLORS.eachWithIndex { color, index ->
            COLORS[index] = color as Color
        }
    }

    static registerColorfulQuadrels() {
		Container.QUADRELS.eachWithIndex { quadrelClass, i ->
			def metaClass = quadrelClass.metaClass

			metaClass.getColor = { -> COLORS[i] }

			def copyOrRemove = { name, color, container, x, y ->
				delegate.metaClass.invokeMethod(delegate.class, delegate, name, [container, x, y] as Object[], true, false)
				delegate.eachSquare { xb, yb ->
					container.setColorAt x + xb, y + yb, color
				}
			}
			metaClass.copyTo = copyOrRemove.curry('copyTo', COLORS[i])
			metaClass.removeFrom = copyOrRemove.curry('removeFrom', null)
		}
	}

}
